package spring.circular_dependencies;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

public class LearnCircularDependencies {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
        A beanA = context.getBean(A.class);
        B beanB = context.getBean(B.class);
        System.out.println(beanA.getA_val());
        System.out.println(beanA.getB().getB_val());
        System.out.println(beanB.getB_val());
        System.out.println(beanB.getA().getA_val());
    }
}

@Configuration
@ComponentScan("spring.circular_dependencies")
class Config {

}

@Component
@Data
class A {
    @Autowired
    private B b;

    private int a_val;

    public A() {
        a_val = 42;
    }

}

@Component
@Data
class B {
    @Autowired
    private A a;

    private int b_val;

    public B() {
        b_val = 24;
    }
}
